package com.controller;


import com.dao.FCartDao;
import com.dao.UserDao;
import com.model.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CartController {
    @Resource
    FCartDao fCartDao;
    @Resource
    UserDao userDao;
    @Resource
    Result result;
    private Result setResult(int status, String msg) {
        result.setStatus(status);
        result.setMsg(msg);
        return result;
    }

    @GetMapping(value = "/showCart")
    public String userCartList(ModelMap model, HttpServletRequest request){
        HttpSession session=request.getSession();
        FUser user=(FUser) session.getAttribute("USER_SESSION");
        //定义字典key :商品分类名称，value:对应分类下的商品列表
        List<FCart>fCartList=fCartDao.getCartByUserId(user.getId());
        //统计购物车物品总价
        double total_price=0;
        for (FCart fc:fCartList){
            total_price+=fc.getbGood().getPrice()*fc.getNum();
        }
//        double total_price=fCartList.stream().map(FCart::getbGood).mapToDouble(BGood::getPrice).sum();
        //统计购物车物品数量
        int total_num =fCartList.stream().mapToInt(FCart::getNum).sum();
        model.addAttribute("fCartList",fCartList);
        model.addAttribute("total_price",String.format("%.2f",total_price));
        model.addAttribute("total_num",total_num);
        model.addAttribute("user",user);
        return "front/cart";
    }

    //后台购物车显示列表
    @GetMapping(value = "/cartList")
    public String cartList(ModelMap model, Page page) {
        //定义字典key :商品分类名称，value:对应分类下的商品列表
        Map<String,List<FCart>> map = new LinkedHashMap<>();
        //获取商品类型列表
        List<FUser> userList =userDao.list(page);
        //遍历商品类型列表，读取对应商品类型的商品数据
        for(FUser fUser:userList){
            List<FCart> cartList=fCartDao.list();
            map.put(fUser.getUserName(),cartList);
        }
        model.addAttribute("userList", userList);
        model.addAttribute("list", map);
        return "backend/cart-list";
    }

    //添加商品加入购物车
    @PostMapping(value = "/addCart")
    @ResponseBody  //ajax请求需要加这个声明
    public Result addCart(FCart fCart, HttpServletRequest request){
        try{
            HttpSession session=request.getSession();
            FUser user=(FUser) session.getAttribute("USER_SESSION");
            fCart.setUser_id(user.getId());
            FCart fCart1=fCartDao.getGood(fCart);
            int row;
            //如果商品已在购物车，则商品数量累加
            if (fCart1!=null){
                int num =fCart1.getNum()+fCart.getNum();
                fCart.setNum(num);
                fCart.setId(fCart1.getId());
                row =fCartDao.updateCart(fCart);
            }else {
                row=fCartDao.insertCart(fCart);
            }
            if (row>0){
                return this.setResult(1,"添加成功");
            }else {
                return this.setResult(0,"添加出错，请联系管理员！");
            }
        }catch (Exception ex){
            return this.setResult(0,ex.getMessage());
        }
    }

    //删除商品
    @PostMapping(value = "/deleteCart")
    @ResponseBody
    public Result deleteCart(Integer id, ModelMap model){
        try{
            FCart fCart=fCartDao.getCartById(id);
            if(fCart==null){
                return this.setResult(0,"用户不存在！");
            }else {
                int row = fCartDao.deleteCart(id);
                if (row > 0) {
                    return this.setResult(1, "删除用户成功！");
                } else {
                    return this.setResult(0, "删除用户出错，请联系管理员！");
                }
            }
        }catch (Exception ex){
            return this.setResult(0,"删除用户出错，请联系管理员！");
        }
    }

}


